package com.meixiang.adapter.shopping;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.meixiang.R;
import com.meixiang.entity.shopping.CartBalanceStoreEntity;
import com.meixiang.tool.Tool;
import com.meixiang.view.LineView;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 结算商品店铺列表
 *
 * @author LZRUI
 */
public class SelectSettleStoreAdapter extends RecyclerView.Adapter<SelectSettleStoreAdapter.StoreViewHolder> {

    private Activity mActivity;
    private List<CartBalanceStoreEntity> storeList = new ArrayList<>();

    private SelectStoreListener selectStoreListener;

    public void setSelectStoreListener(SelectStoreListener selectStoreListener) {
        this.selectStoreListener = selectStoreListener;
    }

    public SelectSettleStoreAdapter(Activity context, List<CartBalanceStoreEntity> storeList) {
        this.mActivity = context;
        this.storeList = storeList;
    }

    @Override
    public StoreViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_settle_store, parent, false);
        return new StoreViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(StoreViewHolder holder, int position) {

        final CartBalanceStoreEntity storeEntity = storeList.get(position);
        if (storeEntity == null) return;
        holder.tvGoodsNum.setText(String.format(mActivity.getString(R.string.total_goods_num_settle_select), storeEntity.getGoodsCount()));
        holder.tvStoreName.setText(storeEntity.getStoreName());
        holder.tvTotalPrice.setText(String.format(mActivity.getString(R.string.price_str), storeEntity.getTotalPrice()));
        holder.tvSettle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Tool.isFastDoubleClick()) return;
                if (selectStoreListener != null) {
                    selectStoreListener.toBalance(storeEntity.getCartIds());
                }
            }
        });

        if (position != 1) {
            holder.line.setMarginLeft(15);
            holder.line.setVisibility(View.VISIBLE);
        } else {
            holder.line.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return storeList == null ? 0 : storeList.size();
    }

    static class StoreViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.line)
        LineView line;
        @Bind(R.id.tv_settle)
        TextView tvSettle;
        @Bind(R.id.tv_store_name)
        TextView tvStoreName;
        @Bind(R.id.tv_goods_num)
        TextView tvGoodsNum;
        @Bind(R.id.tv_total_price)
        TextView tvTotalPrice;

        public StoreViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    /**
     * 结算接口
     */
    public interface SelectStoreListener {
        void toBalance(String cartIds);
    }

}
